// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import FastClick from "fastclick";
import VueRouter from "vue-router";
import {
  AjaxPlugin,
  dateFormat,
  Icon,
  XButton,
  Divider,
  XHeader,
  Actionsheet,
  Alert,
  Tabbar,
  TabbarItem,
  Tab,
  TabItem,
  Toast,
  Msg,
  LoadMore,
  Confirm,
  Grid,
  GridItem,
  Swiper,
  SwiperItem,
  Badge,
  Picker,
  Panel,
  XImg,
  XAddress,
  TransferDom,
  XInput,
  XTextarea,
  XNumber,
  Search,
  ViewBox,
  Selector,
  Cell,
  Group,
  Popup,
  CellBox,
  CellFormPreview,
  CheckIcon,
  Checker,
  CheckerItem,
  PopupPicker,
  FormPreview,
  InlineXNumber
} from "vux";
import App from "./App";
import Home from "./components/Home";
import Homepage from "@/components/homepage/homepage";
import Cart from "@/components/cart/cart";
import Detail from "@/components/detail/detail";
import Login from "@/components/login/login";
import Mycenter from "@/components/mycenter/mycenter";
import OrderDetail from "@/components/orderDetail/orderDetail";
import Payment from "@/components/payment/payment";
import ReceivingAddress from "@/components/receivingAddress/receivingAddress";
import AddAddress from "@/components/addAddress/addAddress";
import SearchList from "@/components/searchList/searchList";
import Setting from "@/components/setting/setting";

Vue.use(VueRouter);
Vue.use(AjaxPlugin);
Vue.directive('transfer-dom', TransferDom);
Vue.component("tab", Tab);
Vue.component("tab-item", TabItem);
Vue.component("tabbar", Tabbar);
Vue.component("tabbar-item", TabbarItem);
Vue.component("x-header", XHeader);
Vue.component("icon", Icon);
Vue.component("x-button", XButton);
Vue.component("divider", Divider);
// Vue.component('flexbox', Flexbox)
// Vue.component('flexbox-item', FlexboxItem)
Vue.component("grid", Grid);
Vue.component("grid-item", GridItem);
Vue.component("view-box", ViewBox);
Vue.component('panel', Panel);
Vue.component("cell", Cell);
Vue.component('group', Group);
Vue.component('popup', Popup);
Vue.component("cell-form-preview", CellFormPreview);
Vue.component("cell-box", CellBox);
Vue.component("check-icon", CheckIcon);
Vue.component("checker", Checker);
Vue.component("checker-item", CheckerItem);
Vue.component("form-preview", FormPreview);
Vue.component("inline-x-number", InlineXNumber);
Vue.component("popup-picker", PopupPicker);
Vue.component("picker", Picker);
Vue.component("selector", Selector);
Vue.component("search", Search);
Vue.component("x-input", XInput);
Vue.component("x-number", XNumber);
Vue.component("x-textarea", XTextarea);
Vue.component("x-address", XAddress);
Vue.component("badge", Badge);
Vue.component("swiper", Swiper);
Vue.component("swiperItem", SwiperItem);
Vue.component("x-img", XImg);
Vue.component("actionsheet", Actionsheet);
Vue.component("alert", Alert);
Vue.component("confirm", Confirm);
Vue.component("load-more", LoadMore);
Vue.component("msg", Msg);
Vue.component("toast", Toast);
Vue.prototype.dateFormat = dateFormat;

const routes = [
  {
    path: "/",
    component: Home,
    children: [
      {
        //首页
        path: "/",
        name: "homepage",
        component: Homepage
      },
      {
        //购物车
        path: "/cart",
        name: "Cart",
        component: Cart,
        //购物车判断用户登陆
        beforeEnter: (to, from, next) => {
          //判断session中是否用户已登陆
          if (sessionStorage.getItem("username")) {
            const url = to.path;
            next();
          } else {
            sessionStorage.setItem("beforeUrl", from.path);
            next({ path: "login" });
          }
        }
      },
      {
        //个人中心
        path: "/mycenter",
        name: "Mycenter",
        component: Mycenter,
        //进入个人中心判断用户登陆
        beforeEnter: (to, from, next) => {
          //判断session中是否用户已登陆
          if (sessionStorage.getItem("username")) {
            const url = to.path;
            // console.log(url)
            next();
          } else {
            sessionStorage.setItem("beforeUrl", from.path);
            next({ path: "login" });
          }
        }
      }
    ]
  },
  {
    //商品详情
    path: "/detail",
    name: "Detail",
    component: Detail
  },
  {
    //登陆注册
    path: "/login",
    name: "Login",
    component: Login
  },

  {
    //订单详情
    path: "/orderDetail",
    name: "OrderDetail",
    component: OrderDetail
  },
  {
    //支付
    path: "/payment",
    name: "Payment",
    component: Payment
  },
  {
    //管理收货地址
    path: "/receivingAddress",
    name: "ReceivingAddress",
    component: ReceivingAddress
  },
  {
    //新增/编辑收货地址
    path: "/addAddress",
    name: "AddAddress",
    component: AddAddress
  },
  {
    //搜索
    path: "/search",
    name: "SearchList",
    component: SearchList
  },
  {
    //设置
    path: "/setting",
    name: "Setting",
    component: Setting
  }
];

const router = new VueRouter({
  routes
});

FastClick.attach(document.body);

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount("#app-box");
